package com.five.wechat.connection;

import com.five.common.been.AesException;
import com.five.common.been.enumbeen.Req_Msg_Type;
import com.five.common.util.WeChatUtil;
import com.five.common.util.XmlUtil;
import com.five.wechat.serviceImp.*;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName ConnectWechat
 * @Author Administrator
 * @Date 2018/12/30 13:14
 * @Version 1.0
 * @Description
 **/
@Controller
public class ConnectWechat {

    @Value("${token}")
    private String token;

    @Autowired
    @Qualifier("textServiceType")
    private TextServiceImp textService;

    @Autowired
    @Qualifier("eventServiceType")
    private EventServiceImp eventService;

    @Autowired
    @Qualifier("imageServiceType")
    private ImageServiceImp imageService;

    @Autowired
    @Qualifier("voiceServiceType")
    private VoiceServiceImp voiceService;

    @Autowired
    @Qualifier("videoServiceType")
    private VideoServiceImp videoService;

    @Autowired
    @Qualifier("linkServiceType")
    private LinkServiceImp linkService;

    @Autowired
    @Qualifier("locationServiceType")
    private LocationServiceImp locationService;

    @Autowired
    @Qualifier("otherServiceType")
    private OtherServiceImp otherService;


    @GetMapping("/connection")
    @ResponseBody
    public String Connection(@RequestParam String signature, @RequestParam String timestamp,
                             @RequestParam String nonce, @RequestParam String echostr) {
        /**
         *@MethodName Connection
         *@Data 2018/12/30 13:15
         *@Author wuxuan
         *@Description 接受微信服务器验证的请求接口
         **/
        System.out.println("开始验证-------微信接入服务");
        String sha1 = null;
        try {
            sha1 = WeChatUtil.getWeChatUtil().getSHA1(token, timestamp, nonce, "");
        } catch (AesException e) {
            e.printStackTrace();
        }
        System.out.println(signature);
        System.out.println(sha1);
        if (sha1.equals(signature)) {
            System.out.println("验证成功------验证结束");
            return echostr;
        } else {
            System.out.println("未知请求------验证结束");
            return null;
        }

    }

    @PostMapping("/connection")
    @ResponseBody
    public String HandleMsg(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
        String xmlMsg = null;
        /**
         *@MethodName HandleMsg
         *@Data 2018/12/30 14:51
         *@Author wuxuan
         *@Description 处理微信服务器的服务
         **/
        Map<String, String> xml = XmlUtil.getXmlUtil().xmlToMap(request);
        String msgType = xml.get("MsgType");
        if (msgType.equals(Req_Msg_Type.text.toString())) {
            xmlMsg = textService.handleAllText(xml);
        } else if (msgType.equals(Req_Msg_Type.image.toString())) {
            xmlMsg = imageService.handleAllImage(xml);
        } else if (msgType.equals(Req_Msg_Type.voice.toString())) {
            xmlMsg = voiceService.handleAllVoice(xml);
        } else if (msgType.equals(Req_Msg_Type.video.toString())) {
            xmlMsg = videoService.handleAllVideo(xml);
        } else if (msgType.equals(Req_Msg_Type.event.toString())) {
            xmlMsg = eventService.handleAllEvent(xml);
        } else if (msgType.equals(Req_Msg_Type.link.toString())) {
            xmlMsg = linkService.handleAllLink(xml);
        } else if (msgType.equals(Req_Msg_Type.location.toString())) {
            xmlMsg = locationService.handleAllLocation(xml);
        } else {
            xmlMsg = otherService.handleOtherCase(xml);
        }
        return xmlMsg;
    }

}
